package app.sceledroid.activity;

import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.app.TabActivity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;
import app.sceledroid.R;
import app.sceledroid.activity.course.DeadlineActivity;
import app.sceledroid.activity.course.ForumActivity;
import app.sceledroid.activity.course.ResourceActivity;

public class CourseActivity extends TabActivity {

	private String URL;
	private String courseName;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.course_tab_view);
		
		Bundle bundle = getIntent().getExtras();
		URL = bundle.getString("URL");
		courseName = bundle.getString("CourseName");
		TextView courseNameLabel = (TextView) findViewById(R.id.courseNameTab);

		courseNameLabel.setText(courseName);

		TabHost tabHost = getTabHost();
		TabHost.TabSpec spec;
		Intent intent;

		intent = new Intent().setClass(this, DeadlineActivity.class);
		intent.putExtra("URL", URL);
		intent.putExtra("CourseName", courseName);
		// Initialize a TabSpec for each tab and add it to the TabHost
		spec = tabHost.newTabSpec("deadlines").setIndicator("Deadline")
				.setContent(intent);
		tabHost.addTab(spec);

		// Do the same for the other tabs
		intent = new Intent().setClass(this, ForumActivity.class);
		intent.putExtra("URL", URL);
		intent.putExtra("CourseName", courseName);
		spec = tabHost.newTabSpec("forums").setIndicator("Forum")
				.setContent(intent);
		tabHost.addTab(spec);
		
		// Do the same for the other tabs
		intent = new Intent().setClass(this, ResourceActivity.class);
		intent.putExtra("URL", URL);
		intent.putExtra("CourseName", courseName);
		spec = tabHost.newTabSpec("resources").setIndicator("Resource")
				.setContent(intent);
		tabHost.addTab(spec);

		tabHost.setCurrentTab(0);

	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.afterlogin_menu, menu);
	    return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		//intent for settings
		Intent intent  = null;
		//toast for others
		Context context = getApplicationContext();
		CharSequence help = "SceleDroid";
		int duration = Toast.LENGTH_LONG;
		Toast toast = Toast.makeText(context, help, duration);
		
	    // Handle item selection
	    switch (item.getItemId()) {
	    case R.id.help:
	       // showHelp();
	    	toast.show();
	        return true;
	    case R.id.settings:
	    	intent = new Intent(CourseActivity.this, SettingsActivity.class);
	    	startActivity(intent);
	    	return true;
	    case R.id.credits:
	    	toast.show();
	    	return true;
	    case R.id.logout:
	    	SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
			Editor e = pref.edit();//this.getPreferences(Context.MODE_PRIVATE).edit();
			e.clear();
			e.commit();
			intent = new Intent(CourseActivity.this, LoginActivity.class);
			intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
	    	startActivity(intent);
	    	return true;
	    case R.id.search:
	    	intent = new Intent(CourseActivity.this, SearchActivity.class);
	    	startActivity(intent);
	    	return true;
	    default:
	        return super.onOptionsItemSelected(item);
	    }
	}
}
